package com.school.base.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.school.base.dao.IAdminDao;
import com.school.base.model.Admin;
import com.school.common.base.BaseDao;
import com.school.common.constant.Constant;
import com.school.common.util.Md5Utils;

public class AdminDao extends BaseDao implements IAdminDao {

	@Override
	public Admin getAdminById(int id) {
		return (Admin) this.getObjectById(Admin.class, id);
	}

	@Override
	public List<Admin> getAdminList(Admin admin, int offset, int pageSize) {
		List<Criterion> express = new ArrayList<Criterion>();
		express.add(Restrictions.ne("status", Constant.ACCOUNT_STATUS_DELETED));
		if (admin != null) {
			if (admin.getRealName() != null && !admin.getRealName().isEmpty()) {
				express.add(Restrictions.like("realName", "%" + admin.getRealName() + "%"));
			}
		}

		return this.getList(Admin.class, express, null, offset, pageSize);
	}

	@Override
	public int getTotalCount(Admin admin) {
		List<Criterion> express = new ArrayList<Criterion>();
		express.add(Restrictions.ne("status", Constant.ACCOUNT_STATUS_DELETED));
		if (admin != null) {
			if (admin.getRealName() != null && !admin.getRealName().isEmpty()) {
				express.add(Restrictions.like("realName", "%" + admin.getRealName() + "%"));
			}
		}
		return this.getRecordCount(Admin.class, express);
	}

	@Override
	public void updateAdmin(Admin admin) {
		String hql = "update Admin set ";
		List express = new ArrayList();
		if (admin.getPwd() != null && !admin.getPwd().isEmpty()) {
			admin.setPwd(Md5Utils.getMd5(admin.getPwd()));
			hql += "pwd=?, ";
			express.add(admin.getPwd());
		}
		hql += "realName=? ";
		express.add(admin.getRealName());
		
		hql += " where id=?";
		express.add(admin.getId());
		this.executeHql(hql, express);
	}

	@Override
	public boolean checkLoginName(String loginName, int adminId) {
		List<Criterion> express = new ArrayList<Criterion>();
		express.add(Restrictions.eq("loginName", loginName));
		express.add(Restrictions.ne("id", adminId));
		List list = this.getList(Admin.class, express, null);
		if (list != null && list.size() > 0) {
			return false;
		}
		return true;
	}

	@Override
	public void deleteAdmin(int id) {
		String hql = "update Admin set status=" + Constant.ACCOUNT_STATUS_DELETED + " where id=" + id;
		this.executeHql(hql);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.school.base.dao.IAdminDao#check(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public Admin check(String loginName, String pwd) throws Exception {
		List<Criterion> express = new ArrayList<Criterion>();
		express.add(Restrictions.eq("loginName", loginName));
		express.add(Restrictions.eq("pwd", pwd));
		List list = this.getList(Admin.class, express, null);
		if (list != null && !list.isEmpty()) {
			Admin admin = (Admin) list.get(0);
			if (Constant.ACCOUNT_STATUS_STOPED == admin.getStatus()) {
				throw new Exception("账户已停用");
			}
			if (Constant.ACCOUNT_STATUS_DELETED == admin.getStatus()) {
				throw new Exception("账户已被删除");
			}
			admin.setLastLoginTime(new Date());
			this.update(admin);
			return admin;
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.school.base.dao.IAdminDao#insertAdmin(com.school.base.model.Admin)
	 */
	@Override
	public void insertAdmin(Admin admin) {
		admin.setCreateTime(new Date());
		admin.setStatus(Constant.ACCOUNT_STATUS_INUSED);
		admin.setPwd(Md5Utils.getMd5(admin.getPwd()));
		this.insert(admin);
	}

}
